/*
package com.dawn.framework.crypto.code.interceptor;

import com.dawn.framework.crypto.code.util.AesUtil;
import com.dawn.framework.crypto.code.wrapper.DecryptHttpServletRequestWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

*/
/**
 * @description:
 * @author: cnsu-cmh
 * @time: 2025/3/11 15:15
 *//*

@Slf4j
public class DecryptInterceptor implements HandlerInterceptor {

    private static final String ENCRYPTED_HEADER = "X-Encrypted";
    private static final String ENCRYPTED_VALUE = "true";

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        String encryptedHeader = request.getHeader(ENCRYPTED_HEADER);
        if (ENCRYPTED_VALUE.equals(encryptedHeader)) {
            try (BufferedReader reader = request.getReader()) {
                // 读取原始请求体
                String body = IOUtils.toString(reader);

                if (body == null || body.isEmpty()) {
                    throw new IllegalArgumentException("Request body is empty");
                }

                // 去除前后引号
                body = body.trim();
                if (body.startsWith("\"") && body.endsWith("\"")) {
                    body = body.substring(1, body.length() - 1);
                }

                // 解密
                String decrypted = AesUtil.decrypt(body);

                // 替换请求对象
                HttpServletRequest wrappedRequest = new DecryptHttpServletRequestWrapper(request, decrypted);

                // 更新请求上下文
                RequestContextHolder.setRequestAttributes(
                        new ServletRequestAttributes(wrappedRequest, response),
                        true // 继承父线程上下文
                );


            } catch (IOException e) {
                log.error("Failed to read request body", e);
                throw new RuntimeException("Failed to read request body", e);
            } catch (Exception e) {
                log.error("Decryption failed", e);
                throw new RuntimeException("Decryption failed", e);
            }
        }
        return true;
    }
}*/
